/*
 * CCVisu is a tool for visual graph clustering
 * and general force-directed graph layout.
 * This file is part of CCVisu.
 *
 * Copyright (C) 2005-2011  Dirk Beyer
 *
 * CCVisu is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * CCVisu is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with CCVisu; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Please find the GNU Lesser General Public License in file
 * license_lgpl.txt or http://www.gnu.org/licenses/lgpl.txt
 *
 * Dirk Beyer    (firstname.lastname@uni-passau.de)
 * University of Passau, Bavaria, Germany
 */
package org.sosy_lab.ccvisu.graph;

import java.awt.Color;

/**
 * Represents an edge between two vertices source and target
 * (given as vertex ids) of a given weight.
 */
public class GraphEdge implements Comparable<GraphEdge> {

  /** Source vertex of edge. */
  private GraphVertex source       = null;

  /** Target vertex of edge. */
  private GraphVertex target       = null;

  /** Edge weight. */
  private float       weight       = 1.0f;

  /** The color of the edge. */
  private Color       color        = Color.BLACK;

  /** The name of the relation the edge belongs to. */
  private String      relationName = "Edge";

  /** Indicates whether this edge is to be painted solid or dashed. */
  private boolean     dashed       = false;

  /**
   * True if the edge shall be displayed. Used for partial display of edges
   * during highlighting.
   */
  private boolean     showEdge     = false;

  /** True if the name shall be annotated in the visualization. */
  private boolean     showName     = false;

  /** The unique id of this edge in a graph. */
  private int         id;

  /**
   * Creates a new GraphEdge.
   *
   * @param relationName the name of the edge
   * @param source the source vertex of the edge
   * @param target the target vertex of the edge
   * @param weight the weight of the edge
   */
  public GraphEdge(String relationName, GraphVertex source, GraphVertex target,
      float weight) {

    this(relationName, source, target, Color.BLACK);
    assert(weight >= 0);

    this.weight = weight;
  }

  /**
   * Copy constructor. Creates a new GraphEdge similar to the old one but
   * sets a different source and target.
   *
   * @param edge the old edge (to be copied)
   * @param source the new source of the edge
   * @param target the new target of the edge
   */
  public GraphEdge(GraphEdge edge, GraphVertex source, GraphVertex target) {

    this(edge.relationName, source, target, edge.color);

    this.weight = edge.weight;
    this.id = edge.id;
    this.showName = edge.showName;
    this.showEdge = edge.showEdge;
    this.dashed = edge.dashed;
  }

  private GraphEdge(String relationName, GraphVertex source, GraphVertex target,
      Color color) {

    assert(relationName != null && source != null && target != null && color != null);

    this.relationName = relationName;
    this.source = source;
    this.target = target;
    this.color = color;
  }

  /**
   * @return The source of this edge.
   */
  public GraphVertex getSource() {
    return source;
  }

  /**
   * @return The target of this edge.
   */
  public GraphVertex getTarget() {
    return target;
  }

  /**
   * @return The name of the relation.
   */
  public String getRelationName() {
    return relationName;
  }

  /**
   * @return The weight of this edge.
   */
  public float getWeight() {
    return weight;
  }

  /**
   * @return the length of this edge
   */
  public float getLength() {
    return getSource().distanceTo(getTarget());
  }

  /**
   * @return the id of this edge
   */
  public int getId() {
    return id;
  }

  /**
   * Updates the id of this edge.
   * @param id the new id; must be >= 0
   */
  public void setId(int id) {
    assert(id >= 0);
    this.id = id;
  }

  /**
   * @return The color of the edge.
   */
  public Color getColor() {
    return color;
  }

  /**
   * @param color The new color of the edge.
   */
  public void setColor(Color color) {
    assert(color != null);
    this.color = color;
  }

  /**
   * @return Whether this edge is to be shown in a graph visualization.
   */
  public boolean showEdge() {
    return showEdge;
  }

  /**
   * Sets the visibility of this edge.
   *
   * @param showEdge <code>true</code> if this edge is to be shown,
   *                 <code>false</code> otherwise.
   */
  public void setShowEdge(boolean showEdge) {
    this.showEdge = showEdge;
  }

  /**
   * Returns whether the relation name of this edge is to be shown in
   * visualizations.
   */
  public boolean showName() {
    return showName;
  }

  /**
   * Set whether the name of this edge is to be shown in visualizations.
   *
   * @param showName <code>true</code> if the relation name is to be shown,
   *                 <code>false</code> otherwise.
   */
  public void setShowName(boolean showName) {
    this.showName = showName;
  }

  /**
   * Returns whether this edge is to be visualized dashed (instead of solid).
   */
  public boolean isDashed() {
    return dashed;
  }

  /**
   * @param dashed <code>true</code> if the edge is to be visualized as a dashed line,
   *               <code>false</code> if it is to be solid.
   */
  public void setDashed(boolean dashed) {
    this.dashed = dashed;
  }

  @Override
  public int compareTo(GraphEdge edge) {
    assert(edge != null);

    int result = new Integer(getSource().getId()).compareTo(edge.getSource().getId());
    if (result != 0) {
      return result;
    }

    result = new Integer(getTarget().getId()).compareTo(edge.getTarget().getId());
    if (result != 0) {
      return result;
    }

    result = new Float(getWeight()).compareTo(edge.getWeight());
    if (result != 0) {
      return result;
    }

    result = new Integer(color.getRGB()).compareTo(edge.color.getRGB());
    if (result != 0) {
      return result;
    }

    result = new Boolean(showName).compareTo(edge.showName);
    if (result != 0) {
      return result;
    }

    result = new Boolean(showEdge).compareTo(edge.showEdge);
    if (result != 0) {
      return result;
    }

    result = relationName.compareTo(edge.relationName);
    return result;
  }
}
